package com.ldk.admin.controller;

import com.ldk.admin.common.LigerUIGridResult;
import com.ldk.admin.common.ResponseResult;
import com.ldk.base.framework.common.CustomPageable;
import com.ldk.framework.entity.RatedDriverpunish;
import com.ldk.framework.entity.RatedPassenger;
import com.ldk.framework.service.IRatedDriverpunishService;
import com.ldk.framework.service.IRatedPassengerService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
@RequestMapping("/rated/driverpunish")
public class RatedDriverpunishController extends BaseController {
    /**
     * 路由地址
     */
    private final String route = "rated/driverpunish";
    /**
     * 模块名称
     */
    private final String module = "驾驶员处罚信息";

    @Autowired
    private IRatedDriverpunishService beanService;

    @RequestMapping("/list")
    //public ModelAndView list(ModelMap map){
    public ModelAndView list(ModelMap map,HttpServletRequest request,
                             HttpServletResponse response){
        //session是否过期
        this.isExpired(request,response);

        setLinks(map, "", "", module+"管理", "/"+route+"/list", "", "");
        return new ModelAndView("/rated/listdriverpunish");
    }

    @ResponseBody
    @RequestMapping("/getList")
    public LigerUIGridResult<RatedDriverpunish> getList(@RequestParam(value="page",required=false,defaultValue="1") int page,
                                                        @RequestParam(value="pagesize",required=false,defaultValue="10") int pageSize,
                                                        @RequestParam(value="searchKey",required=false,defaultValue="") String searchKey,
                                                        HttpServletRequest request)
    {
        int limit = pageSize;
        int offset = (page -1) * pageSize;

        RatedDriverpunish query = new RatedDriverpunish();
        if(!StringUtils.isEmpty(searchKey)){
            query.setSearchKey(searchKey);
        }
        Sort sort = new Sort(Direction.DESC, "PunishTime");
        CustomPageable pageable = new CustomPageable(offset, limit, sort);

        long total = beanService.selectCount("", query);
        List<RatedDriverpunish> result = beanService.selectList("", query, pageable);

        LigerUIGridResult<RatedDriverpunish> grid = new LigerUIGridResult<>();
        grid.Total = total;
        grid.Rows = result;

        return grid;
    }

    @ResponseBody
    @RequestMapping("/remove/{Id}")
    public ResponseResult remove(@PathVariable long Id, HttpServletRequest request){
        ResponseResult rst = new ResponseResult();
        if(Id < 1)
        {
            rst.AddError("删除失败");
            return rst;
        }
        RatedDriverpunish query = new RatedDriverpunish();
        query.setId(Id);

        RatedDriverpunish oplog = beanService.selectOne("", query);
        if(oplog == null){
            rst.AddError("Id不存在");
            return rst;
        }

        int row  = beanService.delete("", oplog);
        if(row > 0){
            rst.AddOk("删除成功");
        }
        else{
            rst.AddError("删除失败");
        }

        return rst;
    }
}
